package com.andy.module.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

/**
 * 垂直分隔线
 * Created by luofan on 2018/9/6.
 */

public class VerticalItemDecoration extends RecyclerView.ItemDecoration {

    private int mVerticalSpace;

    private boolean mHasTopDivider = false;

    private boolean mHasBottomDivider = true;

    private Paint mPaint = new Paint();

    private Paint mBgPaint = new Paint();

    private int mHorizontalMargin;

    private int mBgColor;

    public VerticalItemDecoration(int verticalSpace, int color) {
        mVerticalSpace = verticalSpace;
        mPaint.setColor(color);
    }

    public void setHasBottomDivider(boolean hasBottomDivider) {
        mHasBottomDivider = hasBottomDivider;
    }

    public void setBgColor(int bgColor) {
        mBgColor = bgColor;
        mBgPaint.setColor(mBgColor);
    }

    public void setHorizontalMargin(int horizontalMargin) {
        mHorizontalMargin = horizontalMargin;
    }

    public void setHasTopDivider(boolean hasTopDivider) {
        mHasTopDivider = hasTopDivider;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int start = 0;
        if (parent.getAdapter() instanceof BaseQuickAdapter) {
            BaseQuickAdapter quickAdapter = (BaseQuickAdapter) parent.getAdapter();
            start = quickAdapter.getHeaderLayoutCount();
        }
        int position = parent.getChildAdapterPosition(view);
        if (position < start) {
            outRect.top = 0;
        } else {
            if (!mHasTopDivider && position == start) {
                outRect.top = 0;
            } else {
                outRect.top = mVerticalSpace;
            }
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getAdapter() == null) {
            return;
        }
        int childCount = parent.getChildCount();
        int start = 0;
        if (parent.getAdapter() instanceof BaseQuickAdapter) {
            BaseQuickAdapter quickAdapter = (BaseQuickAdapter) parent.getAdapter();
            start = quickAdapter.getHeaderLayoutCount();
        }
        int itemCount = parent.getAdapter().getItemCount();
        for (int i = start; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int index = parent.getChildAdapterPosition(view);
            //第一个ItemView,没有tapGap则不绘制
            if (index == start && !mHasTopDivider) {
                continue;
            }
            if (index == itemCount - 1 && !mHasBottomDivider) {
                continue;
            }
            float dividerTop = view.getTop() - mVerticalSpace;
            float dividerLeft = parent.getPaddingLeft() + mHorizontalMargin;
            float dividerBottom = view.getTop();
            float dividerRight = parent.getWidth() - parent.getPaddingRight() - mHorizontalMargin;
            if (mBgColor != 0) {
                c.drawRect(0, dividerTop, parent.getWidth(), dividerBottom, mBgPaint);
            }
            c.drawRect(dividerLeft, dividerTop, dividerRight, dividerBottom, mPaint);
        }
    }
}
